﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DataAccess.DataObject;
using System.Data;
using System.Collections.Generic;

namespace EnmineKaitlin.admin
{
    public partial class group : Common.PageBase
    {
        const string module = "manage-user";
        int currentpage = 1;
        int pagesize = 10;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (CheckPermission(module))
            {
                if (!IsPostBack)
                {
                    if (Request["gid"] != null && Request["gid"].ToString() != "")
                    {
                        pnlAddGroup.Visible = false;
                        pnlDetailGroup.Visible = true;
                        rptDetailGroup.DataSource = _BGroup.Get(int.Parse(Request["gid"].ToString()));
                        rptDetailGroup.DataBind();
                        IList<OPermission> lstPermission = _BPermission.Get(int.Parse(Request["gid"].ToString()));
                        foreach (ListItem li in ((CheckBoxList)rptDetailGroup.Items[0].FindControl("cblPermission")).Items)
                        {
                            foreach (OPermission objPermission in lstPermission)
                            {
                                if (li.Value == objPermission.IDModule)
                                {
                                    li.Selected = true;
                                }
                            }
                        }

                    }
                    GetParam();
                    BindGroup();
                }
            }
            else
            {
                Response.Redirect("~/admin/permission-fail.aspx");
            }
        }
        protected void GetParam()
        {
            if (Request["currentpage"] != null && Request["currentpage"].ToString() != "")
            {
                currentpage = int.Parse(Request["currentpage"].ToString());
            }
        }
        protected void BindGroup()
        {
            IList<OGroup> lstGroup=_BGroup.Get(currentpage,pagesize);
            grvGroup.DataSource = lstGroup;
            grvGroup.DataBind();
            if (lstGroup.Count > 0)
            {
                ctlPagging.PageSize = pagesize;
                ctlPagging.ItemCount = (int)lstGroup[0].TotalResult;
                ctlPagging.QueryStringParameterName = "";
                ctlPagging.CurrentIndex = currentpage;
            }
        }
        protected void btnAddGroup_Click(object sender, EventArgs e)
        {
            _OGroup.Name = txtName.Text;
            _OGroup.Description = txtDescription.Text;
            if (_BGroup.Add(_OGroup))
            {
                int GroupID = _BGroup.GetIDLast();
                _OPermission.IDGroup = GroupID;
                foreach (ListItem li in cblPermission.Items)
                {
                    if (li.Selected)
                    {                        
                        _OPermission.IDModule = li.Value;
                        _OPermission.Roles = "full";
                        if (_BPermission.Add(_OPermission))
                        {
                            BindGroup();
                        }
                    }
                }                
            }            
        }        
        protected void grvGroup_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            BoundFunctionGridview(e);
        }
        protected void grvGroup_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                if (_BGroup.Delete(int.Parse(e.CommandArgument.ToString())))
                {
                    BindGroup();
                }
                else {
                    RegisterClientScriptBlock("NOTE", "<script>alert('Còn người dùng thuộc nhóm này, không thể xóa !');</script>");
                }
            }
        }

        protected void rptDetailGroup_Update(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                bool flag = true;
                int GroupID = int.Parse(e.CommandArgument.ToString());
                _OPermission.IDGroup = GroupID;
                _OPermission.Roles = "full";
                if (_BPermission.Delete(GroupID))
                {
                    foreach (ListItem li in ((CheckBoxList)e.Item.FindControl("cblPermission")).Items)
                    {
                        if (li.Selected)
                        {
                            _OPermission.IDModule = li.Value;
                            if (!_BPermission.Add(_OPermission))
                            {
                                flag = false;
                                return;
                            }
                        }
                    }
                }
                if (flag)
                {
                    BindGroup();
                }
            }
        }

        protected void grvGroup_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}
